package com.huirui.server.controller.mini.program;

import com.huirui.server.pojo.result.PageResult;
import com.huirui.server.pojo.result.Result;
import com.huirui.server.pojo.vo.ProductDetailVo;
import com.huirui.server.pojo.vo.ProductDetailedVo;
import com.huirui.server.pojo.vo.ProductEchoVo;
import com.huirui.server.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/product")
@Slf4j
@Validated
@Tag(name = "商品管理", description = "微信小程序商品相关")
public class ProductController {
    @Resource
    private ProductService productService;

    @GetMapping
    @Operation(summary = "根据商品Id获取商品详情信息")
    public Result<ProductDetailVo> getProductDetailInfoByProductId(@RequestParam("id") Long id) {
        ProductDetailVo productDetailVo = productService.getProductDetailInfoByProductId(id);
        return Result.success(productDetailVo);
    }

    @GetMapping("/detail")
    @Operation(summary = "根据商品ID获取商品详情")
    public Result<ProductDetailedVo> getProductDetailedVoByProductId(@RequestParam("id") Long id) {
        ProductDetailedVo productDetailedVo = productService.getProductDetailedVoByProductId(id);
        return Result.success(productDetailedVo);
    }

    @GetMapping("/list")
    @Operation(summary = "搜索商品")
    public Result<PageResult<ProductEchoVo>> listProduct(
            @Parameter(
                    description = "搜索类型，0-关键字搜索，1-分类搜索，2-品牌搜索"
            )
            @RequestParam("searchType") Integer searchType,
            @Parameter(
                    description = "排序方式，0-综合排序，1-销量排序，2-价格从低到高，3-价格从高到低，4-上架时间"
            )
            @RequestParam("sort") Integer sort,
            @Parameter(
                    description = "普通搜索下是关键字，分类搜索或品牌搜索下是分类ID或者品牌ID"
            )
            @RequestParam("keyword") String keyword,
            @RequestParam("page") Long page,
            @RequestParam("pageSize") Long pageSize
    ) {
        PageResult<ProductEchoVo> res = productService.listProduct(searchType, sort, keyword, page, pageSize);
        return Result.success(res);
    }


}
